Page({
  data: {
    isShowFrom1: true,
    isShowFrom2: false,
    isShowFrom3: false,
    date: "",
    date2: "",
    date3: "",
    date4: "",
    date5: "",
    date6: "",
    startDate: "",
    endDate: "",
    typeDate: "",
    timeType: "",
    items: [
      {value: '3',name: '三天内'},
      {value: '7',name: '一周内'},
      {value: '14',name: '两周内'},
      {value: '30',name: '一个月内'},
    ],
    items2: [
      {value: '3',name: '三天内'},
      {value: '7',name: '一周内'},
      {value: '14',name: '两周内'},
      {value: '30',name: '一个月内'},
    ]
  },
  showFrom(e) {
    var param = e.target.dataset.param;
    var that = this;
    that.setData({
      isShowFrom1: param == 1 ? (that.data.isShowFrom1 ? false : true) : false,
      isShowFrom2: param == 2 ? (that.data.isShowFrom2 ? false : true) : false,
      isShowFrom3: param == 3 ? (that.data.isShowFrom3 ? false : true) : false
    });
  },
  radioChange: function(e) {
    var that = this;
    var day = e.detail.value;
    var res = getDay(day);
    that.setData({
      date: res.start,
      date2: res.end,
      date3: "",
      date4: "",
      date5: "",
      date6: "",
      startDate: res.start,
      endDate: res.end,
      typeDate: "到达时间",
      timeType: "1"
    });
    //console.log('radio发生change事件，携带value值为：', e.detail.value);
    var items = that.data.items;
    for (var i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value == e.detail.value;
    }

    that.setData({
      items: items
    });
  },
  radioChange2: function(e) {
    var that = this;
    var day = e.detail.value;
    var res = getDay(day);
    that.setData({
      date: "",
      date2: "",
      date3: res.start,
      date4: res.end,
      date5: "",
      date6: "",
      startDate: res.start,
      endDate: res.end,
      typeDate: "出发时间",
      timeType: "2"
    });
    console.log('radio发生change事件，携带value值为：', e.detail.value);

    var items2 = that.data.items2;
    for (var i = 0, len = items2.length; i < len; ++i) {
      items2[i].checked = items2[i].value == e.detail.value;
    }

    that.setData({
      items2: items2
    });
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      date3: "",
      date4: "",
      date5: "",
      date6: "", 
      startDate: e.detail.value,
      typeDate: "到达时间",
      timeType: "1",
      items: [
        { value: '3', name: '三天内' },
        { value: '7', name: '一周内' },
        { value: '14', name: '两周内' },
        { value: '30', name: '一个月内' },
      ],
      items2: [
        { value: '3', name: '三天内' },
        { value: '7', name: '一周内' },
        { value: '14', name: '两周内' },
        { value: '30', name: '一个月内' },
      ]
    })
  },
  bindDateChange2: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date2: e.detail.value,
      date3: "",
      date4: "",
      date5: "",
      date6: "",
      endDate: e.detail.value,
      typeDate: "到达时间",
      timeType: "1",
      items: [
        { value: '3', name: '三天内' },
        { value: '7', name: '一周内' },
        { value: '14', name: '两周内' },
        { value: '30', name: '一个月内' },
      ],
      items2: [
        { value: '3', name: '三天内' },
        { value: '7', name: '一周内' },
        { value: '14', name: '两周内' },
        { value: '30', name: '一个月内' },
      ]
    })
  },
  bindDateChange3: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: "",
      date2: "",
      date3: e.detail.value,
      date5: "",
      date6: "",
      startDate: e.detail.value,
      typeDate: "出发时间",
      timeType: "2",
      items: [
        { value: '3', name: '三天内' },
        { value: '7', name: '一周内' },
        { value: '14', name: '两周内' },
        { value: '30', name: '一个月内' },
      ],
      items2: [
        { value: '3', name: '三天内' },
        { value: '7', name: '一周内' },
        { value: '14', name: '两周内' },
        { value: '30', name: '一个月内' },
      ]
    })
  },
  bindDateChange4: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: "",
      date2: "",
      date4: e.detail.value,
      date5: "",
      date6: "",
      endDate: e.detail.value,
      typeDate: "出发时间",
      timeType: "2",
      items: [
        { value: '3', name: '三天内' },
        { value: '7', name: '一周内' },
        { value: '14', name: '两周内' },
        { value: '30', name: '一个月内' },
      ],
      items2: [
        { value: '3', name: '三天内' },
        { value: '7', name: '一周内' },
        { value: '14', name: '两周内' },
        { value: '30', name: '一个月内' },
      ]
    })
  },
  bindDateChange5: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: "",
      date2: "",
      date3: "",
      date4: "",
      date5: e.detail.value, 
      startDate: e.detail.value,
      typeDate: "出发-到达",
      timeType: "3",
      items: [
        { value: '3', name: '三天内' },
        { value: '7', name: '一周内' },
        { value: '14', name: '两周内' },
        { value: '30', name: '一个月内' },
      ],
      items2: [
        { value: '3', name: '三天内' },
        { value: '7', name: '一周内' },
        { value: '14', name: '两周内' },
        { value: '30', name: '一个月内' },
      ]
    })
  },
  bindDateChange6: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: "",
      date2: "",
      date3: "",
      date4: "",
      date6: e.detail.value,
      endDate: e.detail.value,
      typeDate: "出发-到达",
      timeType: "3",
      items: [
        { value: '3', name: '三天内' },
        { value: '7', name: '一周内' },
        { value: '14', name: '两周内' },
        { value: '30', name: '一个月内' },
      ],
      items2: [
        { value: '3', name: '三天内' },
        { value: '7', name: '一周内' },
        { value: '14', name: '两周内' },
        { value: '30', name: '一个月内' },
      ]
    })
  },
  sendDate: function () {
    var that = this;
    var pages = getCurrentPages(); // 获取页面栈
    var prevPage = pages[pages.length - 2];
    prevPage.setData({
      timeSlot: that.data.startDate + " ~ " + that.data.endDate,
      typeDate: that.data.typeDate,
      timeType: that.data.timeType,
      startDate: that.data.startDate,
      endDate: that.data.endDate
    });
    wx.navigateBack({
      delta: 1
    });
  }
})

function getDay(day) {
  var now = parseInt(new Date().getTime() / 1000);
  var len = day * 86400;
  //console.log(now);
  //console.log(now + len);
  return {
    'start': fmtDate(now),
    'end': fmtDate(now + len),
  }
}

function fmtDate(inputTime) {
  var date = new Date(inputTime * 1000);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  //console.log(y);

  return y + '-' + m + '-' + d;
}